package newmsg;

import javax.swing.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class GetWeiBoModel {
    public static String getHtmlContent(URL url, String encode) {
        StringBuffer contentBuffer = new StringBuffer();
        int responseCode = -1;
        HttpURLConnection con = null;
        try {
            con = (HttpURLConnection) url.openConnection();
            con.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");// IE代理进行下载
            con.setConnectTimeout(60000);
            con.setReadTimeout(60000);
            // 获得网页返回信息码
            responseCode = con.getResponseCode();
            if (responseCode == -1) {
                System.out.println(url.toString() + " : connection is failure...");
                con.disconnect();
                return null;
            }
            if (responseCode >= 400) // 请求失败
            {
                System.out.println("请求失败:get response code: " + responseCode);
                con.disconnect();
                return null;
            }

            InputStream inStr = con.getInputStream();
            InputStreamReader istreamReader = new InputStreamReader(inStr, encode);
            BufferedReader buffStr = new BufferedReader(istreamReader);

            String str = null;
            while ((str = buffStr.readLine()) != null)
                contentBuffer.append(str);
            inStr.close();
        } catch (IOException e) {
            e.printStackTrace();
            contentBuffer = null;
            System.out.println("error: " + url.toString());
        } finally {
            con.disconnect();
        }
        return contentBuffer.toString();
    }

    public static String getHtmlContent(String url, String encode) {
        try {
            URL rUrl = new URL(url);
            return getHtmlContent(rUrl, encode);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static DefaultListModel getModel() {
        DefaultListModel defaultListModel = new DefaultListModel();
        String weibo_str = getHtmlContent("https://s.weibo.com/top/summary?cate=realtimehot", "utf-8");
        String pattern = "<td class=\"td-02\">(.*?)</td>";
        Pattern r = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE);
        Matcher m = r.matcher(weibo_str);
        int i = 0;
        while (m.find()) {
            String item = m.group(1).trim();
            if (!item.isEmpty()) {
                Pattern item_r_text = Pattern.compile("target=\"_blank\">(.*?)</a>", Pattern.CASE_INSENSITIVE);
                Matcher item_m_text = item_r_text.matcher(item);
                Pattern item_r_num = Pattern.compile("<span>(.*?)</span>", Pattern.CASE_INSENSITIVE);
                Matcher item_m_num = item_r_num.matcher(item);
                String text = "", num = "0";
                if (item_m_text.find()) {
                    text = item_m_text.group(1).trim();
                }
                if (item_m_num.find()) {
                    num = item_m_num.group(1).trim();
                }
                if(text.isEmpty() || text.equals("")) continue;
                if (!num.isEmpty() && Long.valueOf(num) > 0) {
                    defaultListModel.add(i++, text + "（"+num+"）");
                } else {
                    /* 有固定Title 过滤 */
//                    defaultListModel.add(i++, text);
                }
            }
        }
        return defaultListModel;
    }
}
